﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Perceiveit.Xml.Converters
{


    #region public class GuidXConverter : XConverter<Guid>

    public class GuidXConverter : XConverter<Guid>
    {
        private static bool TryParse(string value, out Guid uuid)
        {
            bool success = false;
            try
            {
                uuid = new Guid(value);
                success = true;
            }
            catch (FormatException)
            {
                uuid = Guid.Empty;
            }
            catch (ArgumentException)
            {
                uuid = Guid.Empty;
            }
            catch (OverflowException)
            {
                uuid = Guid.Empty;
            }

            return success;
        }

        public override Guid FromString(string value)
        {
            Guid v;
            if (string.IsNullOrEmpty(value) || !TryParse(value, out v))
                v = Guid.Empty;
            else
            {
                v = new Guid(value);
            }
            return v;
        }

        public override string ToString(Guid value)
        {
            return value.ToString();
        }
    }

    #endregion
}
